//
//  PhotoViewController.m
//  DemoTest
//
//  Created by DaShuEdu on 2017/5/25.
//  Copyright © 2017年 DaShuEdu. All rights reserved.
//

#import "PhotoViewController.h"
#import "UIImage+size.h"

@interface PhotoViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIImageView *imageV;

@end

@implementation PhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 64, 100, 40)];
    [self.backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [self.backBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.backBtn addTarget:self action:@selector(backActon) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.backBtn];
    
    UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [imageBtn setFrame:CGRectMake(200, 64, 100, 40)];
    [imageBtn setTitle:@"相册" forState:UIControlStateNormal];
    [imageBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [imageBtn addTarget:self action:@selector(localPhoto) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:imageBtn];
    
    self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(50, 120, self.view.bounds.size.width-100, 400)];
    self.imageV.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:self.imageV];
}

- (void)backActon {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/** 读取本地图片 */
- (void)localPhoto {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:^{}];
}

#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    BOOL isLeft = NO; // 照片是否是横向的
    // UIImage *cropImage = [info objectForKey:UIImagePickerControllerCropRect];
    
    [picker dismissViewControllerAnimated:YES completion:^{}];
    picker.delegate = nil;
    
    // 裁剪图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    CGFloat editedWidth = image.size.width;
    CGFloat editedHeight = image.size.height;
    // 查看待选择的照片的方向
    if (editedWidth>editedHeight) { // 横屏状态
        isLeft = YES;
    }
    
    if (isLeft) {
        CGRect editRect = CGRectMake((editedWidth-editedHeight)/2.0, 0, editedHeight, editedHeight);
//        CGFloat scale = [[UIScreen mainScreen] scale];
//        CGFloat x = editRect.origin.x*scale;
//        CGFloat y = editRect.origin.y*scale;
//        CGFloat w = editRect.size.width*scale;
//        CGFloat h = editRect.size.height*scale;
//        
//        CGRect clipRect = CGRectMake(x, y, w, h);
//        
//        // 生成新图片
//        CGImageRef sourceImageRef = image.CGImage;
//        CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, clipRect);
//        image = [UIImage imageWithCGImage:newImageRef scale:scale orientation:UIImageOrientationUp];
//        CGImageRelease(newImageRef);
        image = [image imageWithEditRect:editRect];
    }
    
    /* 此处info 有六个值
     * UIImagePickerControllerMediaType; // an NSString UTTypeImage)
     * UIImagePickerControllerOriginalImage;  // a UIImage 原始图片
     * UIImagePickerControllerEditedImage;    // a UIImage 裁剪后图片
     * UIImagePickerControllerCropRect;       // an NSValue (CGRect)
     * UIImagePickerControllerMediaURL;       // an NSURL
     * UIImagePickerControllerReferenceURL    // an NSURL that references an asset in the AssetsLibrary framework
     * UIImagePickerControllerMediaMetadata    // an NSDictionary containing metadata from a captured photo
     */
    /*
     // 保存图片至本地，方法见下文
     __weak EditUserInfoViewController *weakVC = self;
     [weakVC saveImage:image withName:@"123.png"];*/
    //压缩图片
    // UIImage *newImage = [image imageScaledToSize:CGSizeMake(319, 213)];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        image = [image imageByScalingToSize:CGSizeMake(editedWidth, editedWidth)];
    } else {
        image = [image imageScaledToSize:CGSizeMake(editedWidth, editedWidth)];
    }
    //转二进制
    NSData *imageData = UIImageJPEGRepresentation(image,0.7);
    
    /* 上传头像 代码 */
    self.imageV.image = image;
    
    imageData = nil; // 处理imageData
    image = nil;
    info = nil;
}

- (UIImage *)imageWithEditRect:(CGRect)editRect sourceImage:(UIImage *)image
{
    CGFloat scale = [[UIScreen mainScreen] scale];
    CGFloat x = editRect.origin.x*scale;
    CGFloat y = editRect.origin.y*scale;
    CGFloat w = editRect.size.width*scale;
    CGFloat h = editRect.size.height*scale;
    
    CGRect clipRect = CGRectMake(x, y, w, h);
    
    // 生成新图片
    CGImageRef sourceImageRef = image.CGImage;
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, clipRect);
    image = [UIImage imageWithCGImage:newImageRef scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(newImageRef);
    return image;
}

@end
